﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace AutocompleteDemo
{
    internal sealed class AutoCompletionServiceApplicationHost : ServiceHost, IFormattable, IDisposable
	{
        private bool initialized;
        private AutoCompletionServiceApplication m_serviceApp;
        private static AutoCompletionServiceApplicationHost s_current;

        internal AutoCompletionServiceApplicationHost(params Uri[] baseAddressess)
            : base(typeof(AutoCompletionServiceApplication), baseAddressess)
        {
            s_current = this;
        }

        private void Dispose(bool managed)
        {
            
        }

        ~AutoCompletionServiceApplicationHost()
        {
            this.Dispose(false);
        }

        void IDisposable.Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            return string.Format(format, "AutoCompletion Service Host", formatProvider);
        }

        internal static AutoCompletionServiceApplicationHost GetCurrent(AutoCompletionServiceApplication serviceApp)
        {
            if (s_current == null)
            {
                return null;
            }
            if (s_current.m_serviceApp == null)
            {
                s_current.Initialize(serviceApp);
            }
            return s_current;
        }

        private void Initialize(AutoCompletionServiceApplication serviceApp)
        {
            if (!this.initialized)
            {
                this.initialized = true;
                this.m_serviceApp = serviceApp;
            }
        }
	}
}
